package com.we823.project.repository.service.repo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.we823.project.model.repo.MaterialBomModel;
import com.we823.project.repository.dao.repo.MaterialBomRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("materialBomService")
@Scope("prototype")
public class MaterialBomService extends AbstractBaseService<MaterialBomModel> {

	private MaterialBomRepository materialBomRepository;
	
	private static final Logger logger = LoggerFactory.getLogger(MaterialBomService.class);
	@Autowired
	public MaterialBomService(MaterialBomRepository materialBomRepository) {
		super(materialBomRepository);
		this.materialBomRepository = materialBomRepository;
	}
	
	public List<MaterialBomModel> findByCode(String code) {
		return materialBomRepository.findByCode(code);
	}

	public Page<MaterialBomModel> findByCode(String code,Pageable pageable) {
		return materialBomRepository.findByCode(code,pageable);
	}

	public Map<String, MaterialBomModel> getMapByCode(String code) {
		List<MaterialBomModel> materialBomModels = findByCode(code);
		if(materialBomModels!=null && materialBomModels.size()>0) {
			Map<String, MaterialBomModel> map = new HashMap<String, MaterialBomModel>();
			for (MaterialBomModel materialBomModel : materialBomModels) {
				map.put(materialBomModel.getMaterialCode(), materialBomModel);
			}
			return map;
		}
		
		return null;
	}

	public void save(List<MaterialBomModel> materialBomModels) {
		materialBomRepository.save(materialBomModels);
	}

	public Page<MaterialBomModel> findByCodeContaining(String query,
			Pageable pageable) {
		return materialBomRepository.findByCodeContaining(query,pageable);
	}

	public List<MaterialBomModel> findByMaterialCodeAndCode(
			String materialCode, String destCode) {
		return materialBomRepository.findByMaterialCodeAndCode(materialCode,destCode);
	}

	public List<MaterialBomModel> findByProcessAndCode(String process,
			String destCode) {
		return materialBomRepository.findByProcessAndCode(process,destCode);
	}

	public List<MaterialBomModel> findByCodeAndProcess(String materialCode,
			String process) {
		return null;
	}

	/**
	 * code_materialCode_materialDesc 作为key
	 * @return
	 */
	public Map<String, MaterialBomModel> getMapByBomKey() {
		Map<String, MaterialBomModel> map = Maps.newHashMap();
		List<MaterialBomModel> materialBomModels = findAll();
		if (materialBomModels!=null && materialBomModels.size()>0) {
			for (MaterialBomModel materialBomModel : materialBomModels) {
				String key = materialBomModel.getCode()+"_"+materialBomModel.getMaterialCode();
				if(materialBomModel.getMaterialDesc()!=null && !StringUtils.isEmpty(materialBomModel.getMaterialDesc())) {
					key+="_"+materialBomModel.getMaterialDesc();
				}
				map.put(key, materialBomModel);
				
				if (logger.isDebugEnabled()) {
					logger.debug("bom key:{} length:{}",key,key.length());
				}
			}
			return map;
		}
		return null;
	}

	public List<MaterialBomModel> findByModelAndMaterialDesc(String model, String color) {
		return materialBomRepository.findByModelAndMaterialDesc(model,color);
	}

}
